package com.zack.algorithm;

public class ReverseLinked {

    public static void main(String[] args) {
        ReverseLinked reverseLinked = new ReverseLinked();
        LinkList linkList = reverseLinked.insertNode();
        reverseLinked.reverse(linkList);
        linkList.printNodes();
    }

    public void reverse(LinkList linkList) {
        System.out.println("------reverse-----");
        Node curr = linkList.first;
        Node pre = curr.next;
        curr.next = null;
        while (pre != null) {
            Node temp = pre.next;
            pre.next = curr;
            curr = pre;
            pre = temp;
        }
        linkList.first = curr;
    }

    public LinkList insertNode() {
        LinkList linkList = new LinkList();
        linkList.insertNode("Aa");
        linkList.insertNode("Bb");
        linkList.insertNode("Cc");
        linkList.insertNode("Dd");
        linkList.printNodes();
        return linkList;
    }
}

class LinkList {
    Node first;

    public LinkList() {
        first = null;
    }

    public void insertNode(String value) {
        Node node = new Node(value);
        node.next = first;
        first = node;
    }

    public void printNodes() {
        Node node = this.first;
        while (node != null) {
            System.out.println(node.data);
            node = node.next;
        }
    }
}

class Node {

    public String data;
    public Node next;

    public Node(String value) {
        this.data = value;
    }

    public void display() {
        System.out.println(this.data + " ");
    }
}